package com.jerei.store.controller;

import com.jerei.store.entity.BaseEntity;
import com.jerei.store.entity.Order;
import com.jerei.store.service.IOrderService;
import com.jerei.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService service;

    @RequestMapping("/addOrder")
    public JsonResult<Integer> addOrder(Integer aid, long price, HttpSession session){
        System.out.println("aid="+aid+",price="+price);
        String username=getUsernameFromSession(session);
        Integer oid= service.insertOrder(aid,price,username);
        return new JsonResult<>(OK,oid);
    }

    @RequestMapping("/find")
    public JsonResult<Order> findByOid(Integer oid){

        Order order=service.selectByOid(oid);
        System.out.println("order="+order);

        return new JsonResult<>(OK,order);
    }

    @RequestMapping("/update")
    public JsonResult<Void> updateByUid(Integer oid,HttpSession session){
        String username=getUsernameFromSession(session);
        service.updateByOid(oid,username);
        return new JsonResult<>(OK);
    }


}
